<template>
  <PageWrapper class="high-form" title="验收场调">

    <CollapseContainer title="基础信息">
      <BasicForm @register="registerForm">
      </BasicForm>
    </CollapseContainer>
    <CollapseContainer title="流程信息">
      <SelectApprover :id="id" :type="type" @success="getFlowData" />
    </CollapseContainer>
    <a-button type="primary" @click="handleSubmitForm" v-if="serverData.showCommit"> 提交 </a-button> <span> </span>
    <a-button type="primary" @click="handleSaveForm(0)" v-if="serverData.showCommit"> 存草稿 </a-button> <span> </span>
    <a-button type="primary" @click="transferTo()" v-if="serverData.showCommit"> 转交 </a-button>
  </PageWrapper>
</template>

<script lang="ts">
import { defineComponent, ref, onBeforeMount, onMounted, } from 'vue';
import { Button, Tag } from 'ant-design-vue';
import { BasicForm, useForm, ApiSelect } from '/@/components/Form/index';
import { CollapseContainer } from '/@/components/Container';
import { useMessage } from '/@/hooks/web/useMessage';
import { PageWrapper } from '/@/components/Page';
import { useRoute, useRouter } from 'vue-router'

import StaffSelector from '/@/views/components/selector/staffSelector/index.vue';
import SelectApprover from '/@/views/process/SelectApprover.vue';
import { transfer, businessdata } from '/@/api/work/work';
import { formSchema11 } from './data';
import { update,  } from '/@/api/project/collect';

export default defineComponent({
  components: { BasicForm, CollapseContainer, PageWrapper, Button, Tag, ApiSelect, StaffSelector, SelectApprover, },
  emits: ['success', 'register'],
  setup() {
    //主表单
    const [registerForm, { setFieldsValue, getFieldsValue, validate }] = useForm({
      schemas: formSchema11,
      labelWidth: 200,
      showActionButtonGroup: false,
    });

    //流程参数 begin
    const serverDataType: any = { showApprove: false, aaList: [], ccList: [] }
    const serverData = ref(serverDataType);
    const id = ref(0);
    const type = ref('CheckChangDiao2');
    function getFlowData(returnData: any) {
      serverData.value = returnData
    }
    //流程参数 end

    onBeforeMount(() => {
      // 获取路由参数
      const r = useRoute();
      let idStr = r.params.id
      if (typeof (idStr) == 'string') {
        id.value = parseInt(idStr, 10)
      } else {
        id.value = parseInt(idStr[0], 10)
      }
    })

    onMounted(() => {
      if (id.value > 0) {
        setFieldsValue(serverData)
        businessdata({ id: id.value, type: type.value }).then(res => {
          setFieldsValue(res)
          console.log('获取【验收场调】数据成功', res)
        })
      }
    })

    const { createMessage } = useMessage();
    const router = useRouter();
    //提交，需要选择审批人
    async function handleSubmitForm() {
      if (serverData.value.aaList == null || serverData.value.aaList.length == 0) {
        createMessage.success({
          content: '请选择审批人',
          duration: 1,
        });
        return
      }
      handleSaveForm(1)
      router.push('/work/work-done');//跳转已处理
    }
    async function handleSaveForm(saveType) {
      let content = '存草稿成功'
      const values = await validate();
      values['formType'] = type.value
      values['isCommit'] = false
      if (saveType === 1) {
        content = '提交成功'
        values['isCommit'] = true
        values['aaList'] = serverData.value.aaList
        values['ccList'] = serverData.value.ccList
      }
      let delivery = {
        surveyAccept: values['delivery.surveyAccept'],
        id: values['delivery.id'],
      }
      values['delivery'] = delivery

      await update(values);
      createMessage.success({
        content: content,
        duration: 1,
      });
    }
    //转交
    async function transferTo() {
      const values = getFieldsValue();
      if (values['transferTo'] == null ) {
        createMessage.success({
          content: '请选择转交人员',
          duration: 1,
        });
        return
      }

      if (values['transferTo'] == serverData.value.work.task.operator) {
        createMessage.success({
          content: '不能把任务转交给自己',
          duration: 1,
        });
        return
      }
      let content = '转交成功'
      let taskId = serverData.value.work.task.id
      await transfer({ taskId: taskId,  transferTo: values['transferTo'] });
      createMessage.success({
        content: content,
        duration: 1,
      });
      location.reload();
    }
   

    return {
      registerForm, handleSaveForm, handleSubmitForm, transferTo,
      getFlowData, serverData, id, type
    };
  },
});
</script>
